const { ccclass, property } = cc._decorator;
declare let eManager,dbh,DBManager,i18n,JsonHttp;
import BaseView from "../../component/BaseView";
import HzMiniSelectIconList from "./HzMiniSelectIconList";
import UIHelper = require("../../component/UIHelper");

import DrawOne from "../DrawOne";
@ccclass
export default class RepairEarThreeView extends BaseView {
    @property(cc.Node)
    bg: cc.Node = null;
    @property(HzMiniSelectIconList)
    selectList: HzMiniSelectIconList = null;
    //第一point的  提示点击spine
    @property(cc.Node)
    tipsNode: cc.Node = null;
    @property(sp.Skeleton)
    pointSpine1: sp.Skeleton = null;
    @property(sp.Skeleton)
    pointSpine2: sp.Skeleton = null;
    @property(sp.Skeleton)
    pointSpine3: sp.Skeleton = null;
    @property(sp.Skeleton)
    pointSpine4: sp.Skeleton = null;
    @property(cc.Node)
    endNode: cc.Node = null;
    @property(cc.Node)
    btnClose: cc.Node = null;

    @property(cc.Node)
    nieziNode: cc.Node = null;
    @property(cc.Node)
    pointSelect2: cc.Node = null;
    @property(cc.Node)
    pointSelect3: cc.Node = null;
    @property(DrawOne)
    earDraw: DrawOne = null;
    @property(cc.Node)
    pointGuide3: cc.Node = null;
    @property(cc.Node)
    turnAniNode: cc.Node = null;

    _point = 1;
    _isPlay = false;
    _isDone = false;
    _Step = 1;
    _StepNum = 4;
    _isStart = false;//是否是剧情前触发
    _startPosx = null;
    tipPos = [cc.v2(220,50),cc.v2(-220,50),cc.v2(183,-90),cc.v2(220,30),cc.v2(-195,-90),cc.v2(-220,30)];
    onLoad() {
        super.onLoad();
        this.bg.on(cc.Node.EventType.TOUCH_START, this.onTouchStart, this);
        this.bg.on(cc.Node.EventType.TOUCH_MOVE, this.onTouchMove, this);
        this.bg.on(cc.Node.EventType.TOUCH_END, this.onTouchEnd, this);
        //@ts-ignore
        this._isStart = this.node.openParam;
    };

    onClose(){
    };
    start(){
        super.start();
        this.btnClose.active = !this._isStart;
        this.tipsNode.active = true;
        this.tipsNode.x = this.tipPos[0].x;
        this.tipsNode.y = this.tipPos[0].y;
    };

    onTouchStart(event) {
        if(this._isDone)
            return;
        if(this._isPlay)
            return;
        let worldPoint = event.touch.getLocation();
        switch (this._point) {
            case 7:
                let localPoint = this.pointSpine2.node.parent.convertToNodeSpaceAR(worldPoint);
                this.nieziNode.x = localPoint.x;
                this.nieziNode.y = localPoint.y;
                break;
            case 8:
                this.earDraw._touchStart(event,cc.v2(-100,100));
                break;
            case 9:
                this._startPosx = worldPoint;
                break;
            case 10:
                let state = this.pointSpine4.getState();
                if(state){
                    if(state.time > 0){
                        state.play();
                    }else{
                        this.pointSpine4.setAnimation(0,"01",false);
                        this.pointSpine4.setCompleteListener(()=>{
                            this._isDone = true;
                            this.turnAniNode.active = false;
                            this.pointSpine4.setAnimation(0,"02",false);
                            this.pointSpine4.setCompleteListener(()=>{
                                this.endNode.active = true;
                            });
                        });
                    }
                }
                break;
            default:
                break;
        }

    };
    onTouchMove(event) {
        if(this._isDone)
            return;
        if(this._isPlay)
            return;
        let worldPoint = event.touch.getLocation();
        switch (this._point) {
            case 7:
                let localPoint = this.pointSpine2.node.parent.convertToNodeSpaceAR(worldPoint);
                this.nieziNode.x = localPoint.x;
                this.nieziNode.y = localPoint.y;
                let peng = this.pointSelect2.getBoundingBoxToWorld();
                let box = this.nieziNode.getBoundingBoxToWorld();
                if (peng.intersects(box)) {
                    this._isPlay = true;
                    this.nieziNode.active = false;
                    this.pointSelect2.active = false;
                    this.pointSpine2.setAnimation(0,"animation",false);
                    this.pointSpine2.setCompleteListener(()=>{
                        this._point ++;
                        this._isPlay = false;
                        this.pointSpine2.node.parent.active = false;
                        this.earDraw.node.parent.active = true;
                    });
                }
                break;
            case 8:
                this.earDraw._touchMove(event);
                break;
            default:
                break;
        }
    };
    onTouchEnd(event) {
        if(this._isDone)
            return;
        if(this._isPlay)
            return;
        let endPosx = event.touch.getLocation();
        switch (this._point) {
            case 8:
                this.earDraw._touchEnd(() => {
                    this._point ++;
                    this.earDraw.node.parent.active = false;
                    this.pointSpine3.node.parent.active = true;
                });
                break;
            case 9:
                if ((endPosx.y - this._startPosx.y) > 50){
                    this._isPlay = true;
                    this.pointGuide3.active = false;
                    this.pointSelect2.active = false;
                    this.pointSpine3.setAnimation(0,"01",false);
                    this.pointSpine3.setCompleteListener(()=>{
                        this.pointSpine3.node.parent.active = false;
                        this.pointSpine4.node.parent.active = true;
                        this.pointSpine4.setCompleteListener(()=>{
                            this._isPlay = false;
                            this.turnAniNode.active = true;
                            this._point ++;
                        });
                        this.pointSpine4.setAnimation(0,"00",false);
                    });
                }
                break;
            case 10:
                // let state = this.pointSpine4.getState();
                // state.pause();
                break;
            default:
                break;
        }
    };

    //点击tip提示 播放动画
    onClickTips(){
        this.tipsNode.active = false;
        this.pointSpine1.setAnimation(0, "0" + this._point, false);
        this.pointSpine1.setCompleteListener(() => {
            this._point ++;
            this.tipsNode.active = true;
            if(this.tipPos[this._point - 1]){
                this.tipsNode.x = this.tipPos[this._point - 1].x;
                this.tipsNode.y = this.tipPos[this._point - 1].y;
            }
            if(this._point > 6){
                this.pointSpine1.node.parent.active = false;
                this.pointSpine2.node.parent.active = true;
                this.pointSelect2.active = true;
            }
        });
    };

    onClickEnd(isSkip){
        let next = this.params.next && this.params.next.shift();
        if (next) {
            UIHelper.openUI(next, {
                next: this.params.next
            })
            this.node.destroy();
        }
    };

    onClickSkip(){
        this.onClickEnd(true);
    };
}
